package com.lsp.lib_uikit.commonrecycler;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.lsp.lib_uikit.commonrecycler.defaultview.LoadingMoreFooterBase;
import com.lsp.lib_uikit.commonrecycler.utils.WrapperUtils;

/**
 * adapter for  loadmore recyclerview
 * notice: loadmore dont effect RecyclerView.Adapter
 *
 * @author lsp
 * @created 16/10/12 14:51
 */
public class LoadMoreWrapper extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  public static final int ITEM_TYPE_LOAD_MORE = Integer.MAX_VALUE - 2;
  private RecyclerView.Adapter<RecyclerView.ViewHolder> mInnerAdapter;//这个是填充数据的正常adapter
  private View mLoadMoreView;
  private int mLoadMoreLayoutId;

  public LoadMoreWrapper(RecyclerView.Adapter<RecyclerView.ViewHolder> mInnerAdapter) {
    this.mInnerAdapter = mInnerAdapter;
  }

  /**
   * 是否有加载更多的view
   */
  private boolean hasLoadMore() {
    return mLoadMoreView != null || mLoadMoreLayoutId != 0;
  }

  /**
   * 是否显示加载更多的view
   */
  private boolean isShowLoadMore(int position) {
    return hasLoadMore() && (position >= mInnerAdapter.getItemCount());
  }

  @Override public int getItemViewType(int position) {
    if (isShowLoadMore(position)) {
      return ITEM_TYPE_LOAD_MORE;
    }
    return mInnerAdapter.getItemViewType(position);
  }

  @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == ITEM_TYPE_LOAD_MORE) {
      CommonViewHolder holder;
      if (mLoadMoreView != null) {
        holder = new CommonViewHolder(mLoadMoreView);
      } else {
        View itemView =
            LayoutInflater.from(parent.getContext()).inflate(mLoadMoreLayoutId, parent, false);
        holder = new CommonViewHolder(itemView);
      }
      return holder;
    }
    return mInnerAdapter.onCreateViewHolder(parent, viewType);
  }

  @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (isShowLoadMore(position)) {
      return;
    }
    mInnerAdapter.onBindViewHolder(holder, position);
  }

  @Override public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    WrapperUtils.onAttachedToRecyclerView(mInnerAdapter, recyclerView,
        new WrapperUtils.SpanSizeCallback() {

          @Override public int getSpanSize(GridLayoutManager layoutManager,
              GridLayoutManager.SpanSizeLookup oldLookup, int position) {
            if (isShowLoadMore(position)) {
              return layoutManager.getSpanCount();
            }
            if (oldLookup != null) {
              return oldLookup.getSpanSize(position);
            }
            return 1;
          }
        });
  }

  @Override public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
    super.onViewAttachedToWindow(holder);
    if (isShowLoadMore(holder.getLayoutPosition())) {
      setFullSpan(holder);
    }
    mInnerAdapter.onViewAttachedToWindow(holder);
  }

  @Override public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
    mInnerAdapter.onDetachedFromRecyclerView(recyclerView);
  }

  @Override public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
    mInnerAdapter.onViewDetachedFromWindow(holder);
  }

  @Override public void onViewRecycled(RecyclerView.ViewHolder holder) {
    mInnerAdapter.onViewRecycled(holder);
  }

  @Override public boolean onFailedToRecycleView(RecyclerView.ViewHolder holder) {
    return mInnerAdapter.onFailedToRecycleView(holder);
  }

  @Override public void unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
    mInnerAdapter.unregisterAdapterDataObserver(observer);
  }

  @Override public void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
    mInnerAdapter.registerAdapterDataObserver(observer);
  }

  private void setFullSpan(RecyclerView.ViewHolder holder) {
    ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();

    if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams) {
      StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;

      p.setFullSpan(true);
    }
  }

  @Override public int getItemCount() {
    return mInnerAdapter.getItemCount() + (hasLoadMore() ? 1 : 0);
  }

  /**
   * 设置加载更多view
   */
  public LoadMoreWrapper setLoadMoreView(LoadingMoreFooterBase loadMoreView) {
    mLoadMoreView = loadMoreView;
    return this;
  }

  class CommonViewHolder extends RecyclerView.ViewHolder {

    public CommonViewHolder(View itemView) {
      super(itemView);
    }
  }
}
